<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsData">
      <!-- 面包屑 -->
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item to="/">{{
          goodsData.categories[1].name
        }}</xtx-bread-item>
        <xtx-bread-item to="/">{{
          goodsData.categories[0].name
        }}</xtx-bread-item>
        <xtx-bread-item>{{ goodsData.name }}</xtx-bread-item>
      </xtx-bread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <GoodsImage :images="goodsData.mainPictures" />
          <goodsSales />
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <goodsName :goods="goodsData" />
          <!-- 👇组件规格 -->
          <GoodsSku @changeSKU="changeSKU" :goods="goodsData" />
          <xtx-numbox
            label="数量"
            :max="goodsData.inventory"
            v-model="count"
          ></xtx-numbox>
          <xtx-button @click="addCart" type="primary" style="margin-top: 20px"
            >加入购物车</xtx-button
          >
        </div>
      </div>

      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goodsData.id" />

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <xtx-tabs v-model="tableActive">
            <xtx-panes label="商品详情">
              <goodsDetail :detail="goodsData.details"></goodsDetail>
            </xtx-panes>
            <xtx-panes label="商品评价" :hotNumber="goodsData.commentCount">
              <goodsComment></goodsComment>
            </xtx-panes>
          </xtx-tabs>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goodsWarn></goodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goodsHot :goodsId="goodsData.id" :type="1"></goodsHot>
          <goodsHot :goodsId="goodsData.id" :type="2"></goodsHot>

        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="XtxGoodsPage">
import { watch, ref } from "vue";
import { useRoute } from "vue-router";
import { findGoods } from "@/api/goods";
import GoodsRelevant from "./components/goods-relevant.vue";
import GoodsImage from "./components/goods-image.vue";
import goodsSales from "./components/goods-sales.vue";
import goodsName from "@/views/Goods/components/goods-name";
import GoodsSku from "./components/goods-sku.vue";
import goodsDetail from "./components/goods-detail.vue";
import goodsComment from "@/views/Goods/components/goods-comment";
import goodsHot from "./components/goods-hot.vue";
import goodsWarn from "./components/goods-warn.vue";
import { useStore } from "vuex";

const STORE = useStore()

const useGoods = () => {
  const goodsData = ref(null);
  const ROUTE = useRoute();
  watch(
    () => ROUTE.params.id,
    async (id) => {
      if (ROUTE.path !== `/product/${id}`) return;
      goodsData.value = null;
      const { result } = await findGoods({ id: id });
      goodsData.value = result;
      // 跳转商品页面后重置layout导航栏选中情况👇
      const activeItem = result.categories.find(v => v.layer === 1)
      activeItem.activeChild = result.categories.find(v => v.layer === 2)
      STORE.commit('category/setActiveItem', activeItem)
    },
    { immediate: true }
  );
  return goodsData;
};

// 全局GoodsData👇
const goodsData = useGoods();

const changeSKU = (sku) => {
  goodsData.value.price = sku.price;
  goodsData.value.oldPrice = sku.oldPrice;
  goodsData.value.inventory = sku.inventory;
};

// 商品数量
const count = ref(1);

// 加入购物车
const addCart = () => {
  console.log("click");
};

// 商品介绍/评价
const tableActive = ref(0);
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
